{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import glob\n",
    "import xml.etree.ElementTree as ET\n",
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n",
    "import json\n",
    "\n",
    "import h5py\n",
    "import numpy as np\n",
    "import cv2\n",
    "from utils import *\n",
    "from matplotlib import cm as CM\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "root = '/home/leeyh/Downloads/data/ILSVRC/Data/VID/'#set it to your ILSVRC root"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a = 'train/ILSVRC2015_VID_train_0000'\n",
    "b = 'train/ILSVRC2015_VID_train_0001'\n",
    "c = 'train/ILSVRC2015_VID_train_0002'\n",
    "d = 'train/ILSVRC2015_VID_train_0003'\n",
    "e = 'val'\n",
    "name_sets = [a,b,c,d,e]\n",
    "root_sets = []\n",
    "for name_set in name_sets:\n",
    "    root_sets.append(os.path.join(root,name_set))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sequence_paths = []\n",
    "for sequences in root_sets:\n",
    "    for sequence in glob.glob(os.path.join(sequences, '*')):\n",
    "        sequence_paths.append(sequence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "sequences_info = []\n",
    "for sequence_path in sequence_paths:\n",
    "    print sequence_path\n",
    "    img_paths = []\n",
    "    lab_paths = []\n",
    "    for img_path in glob.glob(os.path.join(sequence_path, '*.JPEG')):\n",
    "        img_paths.append(img_path)\n",
    "    img_paths.sort()\n",
    "    for img_path in img_paths:\n",
    "        lab_paths.append(img_path.replace('.JPEG','.xml').replace('Data','Annotations'))\n",
    "    track_ids = []\n",
    "    #find max obj num\n",
    "    obj_num = 0\n",
    "    for lab_path in lab_paths:\n",
    "\n",
    "        in_file = open(lab_path)\n",
    "        tree=ET.parse(in_file)\n",
    "        root = tree.getroot()\n",
    "        for obj in root.iter('object'):\n",
    "            track_id = obj.find('trackid').text\n",
    "            track_id = int(track_id)+1\n",
    "            if track_id > obj_num:\n",
    "                obj_num = track_id\n",
    "    #init track_ids\n",
    "    for i in xrange(obj_num):\n",
    "        track_ids.append([])\n",
    "    for i in xrange(len(lab_paths)):\n",
    "        in_file = open(lab_paths[i])\n",
    "        tree=ET.parse(in_file)\n",
    "        root = tree.getroot()\n",
    "        for obj in root.iter('object'):\n",
    "            track_id = obj.find('trackid').text\n",
    "            xmlbox = obj.find('bndbox')\n",
    "            b = [float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)]\n",
    "            track_ids[int(track_id)].append([img_paths[i],b])\n",
    "    sequences_info.append(track_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open('ilsvrc_vid.txt', 'w') as outfile:\n",
    "    json.dump(sequences_info, outfile)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open('ilsvrc_vid.txt', 'r') as outfile:\n",
    "    root = json.load(outfile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ran_seq = random.randint(0,len(sequences_info)-1)\n",
    "print ran_seq\n",
    "sequence = sequences_info[ran_seq]\n",
    "ran_id = random.randint(0,len(sequence)-1)\n",
    "print ran_id\n",
    "track_obj = sequence[ran_id]\n",
    "ran_f = random.randint(0,len(track_obj)-1)\n",
    "track_f = track_obj[ran_f]\n",
    "b = track_f[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "newlines = []\n",
    "for i in xrange(len(sequences_info)):\n",
    "    sequence = sequences_info[i]\n",
    "    ran_id = random.randint(0,len(sequence)-1)\n",
    "    while len(sequence[ran_id])<2:\n",
    "        ran_id = random.randint(0,len(sequence)-1)\n",
    "    track_obj = sequence[ran_id]\n",
    "    \n",
    "    \n",
    "    ran_f1 = random.randint(0,len(track_obj)-1)\n",
    "\n",
    "    ran_f2 = random.randint(0,len(track_obj)-1)\n",
    "    newlines.append([track_obj[ran_f1],track_obj[ran_f2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "random.shuffle(newlines) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "newlines[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sequence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "img = Image.open(track_f[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.imshow(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.imshow(img.crop((b[0],b[2],b[1],b[3])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "track_obj"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
